package com.growthsee.rider.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.growthsee.common.core.controller.BaseController;
import com.growthsee.common.core.domain.AjaxResult;
import com.growthsee.rider.domain.RiderInfo;
import com.growthsee.rider.domain.view.RiderBankView;
import com.growthsee.rider.enumeration.YesNoEnum;
import com.growthsee.rider.service.RiderInfoService;
import com.growthsee.rider.service.RiderLoanInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;

@AllArgsConstructor
@RestController
@RequestMapping("/rider/out")
@Api("外部接口")
public class RiderOutController extends BaseController {

    private final RiderInfoService riderInfoService;
    private final RiderLoanInfoService riderLoanInfoService;

    @ApiOperation("骑手信息核实")
    @PostMapping
    @ApiImplicitParam(name = "骑手信息核实", value = "骑手信息核实", dataType = "RiderCompany")
    public AjaxResult checkUserInfo(@RequestParam String idCardNo) {
        RiderInfo riderInfo =riderInfoService.getOne(Wrappers.<RiderInfo>lambdaQuery()
                .eq(RiderInfo::getDeleted, YesNoEnum.NO.getValue())
                .eq(RiderInfo::getIdCard,idCardNo)
        );
        RiderBankView riderBankView = new RiderBankView();
        riderBankView.setIdCardNo(idCardNo);
        if(Objects.isNull(riderInfo)){
            riderBankView.setBindStat(0);
            riderBankView.setUserStat(0);
        }else {
            riderBankView.setBindStat(riderInfo.getRegisterStatus());
            riderBankView.setUserStat(riderInfo.getStatus());
        }
        return AjaxResult.success(riderBankView);
    }

    @ApiOperation("借款同步信息")
    @PostMapping("syncLoan")
    @ApiImplicitParam(name = "借款同步信息", value = "借款同步信息", dataType = "RiderCompany")
    public AjaxResult syncLoan(@RequestBody Map body) {
        if(MapUtils.isEmpty(body) || StringUtils.isEmpty(MapUtils.getString(body,"loanInfo","")))return AjaxResult.error("loanInfo未必传值");
        return  riderLoanInfoService.syncLoan(MapUtils.getString(body,"loanInfo",""));
    }


    @ApiOperation("解析数据")
    @PostMapping("analysis")
    @ApiImplicitParam(name = "借款同步信息", value = "借款同步信息", dataType = "RiderCompany")
    public AjaxResult analysis(@RequestBody Map map) {
        return  riderLoanInfoService.analysis(MapUtils.getString(map,"loanInfo"));
    }

    @ApiOperation("处理数据")
    @GetMapping("handler")
    @ApiImplicitParam(name = "处理数据", value = "处理数据", dataType = "RiderCompany")
    public AjaxResult handler() {
        return  riderLoanInfoService.handler();
    }



}
